package com.jjclub.shiro;

import ch.qos.logback.core.encoder.ByteArrayUtil;
import com.jjclub.entity.JjclubMenu;
import com.jjclub.entity.JjclubRole;
import com.jjclub.entity.JjclubUser;
import com.jjclub.mapper.JjclubMenuMapper;
import com.jjclub.mapper.JjclubRoleMapper;
import com.jjclub.mapper.JjclubUserMapper;
import com.jjclub.util.UserUtil;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Classname JcRelam
 * @Description TODO
 * @Date 2019/10/25 13:07
 * @Created by jq
 */
@Service
public class JcRelam extends AuthorizingRealm {

    @Autowired
    private JjclubUserMapper jjclubUserMapper;

    @Autowired
    private JjclubRoleMapper jjclubRoleMapper;

    @Autowired
    private JjclubMenuMapper jjclubMenuMapper;

    /***
     * description:授权
     * @param: [principalCollection]
     * @author: jj
     * @date: 2019/10/25 13:08
     * @return org.apache.shiro.authz.AuthorizationInfo
     **/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        JjclubUser user = (JjclubUser) getAvailablePrincipal(principalCollection);
        List<JjclubRole> jjclubRoleList = jjclubRoleMapper.getAllRoleByUserid(user.getLoginid());
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //添加角色到shiro授权中
        for (JjclubRole jjclubRole:jjclubRoleList) {
            info.addRole(jjclubRole.getRolename());
        }
        List<JjclubMenu> jjclubMenus = jjclubMenuMapper.findAllMenuByUserid(user.getLoginid());
        //权限添加到shiro中
        for(JjclubMenu jjclubMenu:jjclubMenus){
            if(jjclubMenu.getPermission()!=null){
                info.addStringPermission(jjclubMenu.getPermission());
            }
        }
        return info;
    }
    /***
     * description:认证
     * @param: [authenticationToken]
     * @author: jj
     * @date: 2019/10/25 13:09
     * @return org.apache.shiro.authc.AuthenticationInfo
     **/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        JjclubUser user =jjclubUserMapper.findByLoginid(token.getUsername());

        if (user != null) {
            if(user.getStatus() == UserUtil.STATUS_LOCK) {
                throw new LockedAccountException("账户已锁定");
            }
            if(user.getStatus() == UserUtil.STATUS_DISUSE) {
                throw new DisabledAccountException("账户已停用");
            }
            if(user.getStatus() != UserUtil.STATUS_NORMAL) {
                throw new IllegalStateException("不合法的用户状态");
            }
            ByteSource byteSource = ByteSource.Util.bytes(user.getSalt());
            return new SimpleAuthenticationInfo(user,user.getPassword().toCharArray(),byteSource, getName());
        } else {
            throw new UnknownAccountException("没有当前用户");
        }
    }
}
